﻿
@using IOA.MES.DataAccess.Enum
@{
    ViewBag.Title = "返工单管理";
    Layout = "~/Views/Shared/_LayoutPage.cshtml";

    var workshops = ViewBag.Workshops as List<IOA.MES.DataAccess.VModel.VWorkshop>;
    var processStatuses = ViewBag.ProcessStatusEnum as List<KeyValuePair<int, string>>;
    var reviewStatuses = ViewBag.ReviewStatusEnum as List<KeyValuePair<int, string>>;
    var reworkTypes = ViewBag.ReworkTypes as List<KeyValuePair<int, string>>;
}
@section Contents{
    <style>
        .layui-form-label {
            width: auto;
        }

        .layui-table-cell {
            height: auto;
        }
    </style>
}

<div id="page"></div>

<script type="text/template" id="tplPage">
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.ReworkWorkshop">返工车间</label>
                        <div class="layui-input-inline">
                            <select id="selWorkshops" lay-filter="selWorkshops">
                                <option value="">所有车间</option>
                                @foreach (var item in workshops)
                                {
                                    <option value="@item.WorkshopCode">@item.WorkshopName</option>
                                }
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.OrderNo">订单号</label>
                        <div class="layui-input-inline">
                            <input type="text" id="txtOrderNo" placeholder="请输入订单号" value="@ViewBag.OrderNo" @(!string.IsNullOrWhiteSpace(ViewBag.OrderNo) ? " disabled " : "") autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.ReworkNo">返工单号</label>
                        <div class="layui-input-inline">
                            <input type="text" id="txtReWorkNo" placeholder="请输入返工单号" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.ReworkType">返工类型</label>
                        <div class="layui-input-inline">
                            <select id="selWorkshops" lay-filter="selWorkshops">
                                <option value="">所有类型</option>
                                @foreach (var item in reworkTypes)
                                {
                                    <option value="@item.Key">@item.Value</option>
                                }
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.ApprovalStatus">审核状态</label>
                        <div class="layui-input-inline">
                            <select id="selReviewStatus" lay-filter="selReviewStatus">
                                <option value="">所有状态</option>
                                @foreach (var item in reviewStatuses)
                                {
                                    <option value="@item.Key">@item.Value</option>
                                }
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.ProduceStatus">生产状态：</label>
                        <div class="layui-input-inline">
                            <select id="selProduceStatus" lay-filter="selProduceStatus">
                                <option value="">所有状态</option>
                                @foreach (var item in processStatuses)
                                {
                                    <option value="@item.Key">@item.Value</option>
                                }
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label" v-html="lang.Field.ReworkDate">返工日期：</label>
                        <div class="layui-input-inline">
                            <input type="text" id="txtRangeDate" placeholder="请选择工单日期" value="@(DateTime.Today.AddDays(-7).ToString("yyyy-MM-dd")+" 至 "+DateTime.Today.ToString("yyyy-MM-dd"))" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button id="btnSearch" class="layui-btn layuiadmin-btn-list" type="submit" lay-submit lay-filter="submit">
                            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                        </button>
                        <button class="layui-btn layui-btn-normal layuiadmin-btn-list" type="button" onclick="editReworkOrder('');">
                            <i class="layui-icon layui-icon-addition layuiadmin-button-btn"></i>
                        </button>
                    </div>
                </div>
            </div>
        </div>

        <div class="layui-card">
            <div class="layui-card-body">
                <table id="table" lay-filter="table"></table>
            </div>
        </div>
    </div>
</script>


@section Scripts{
    <script>
        var vm = new Vue({
            el: "#page",
            template: "#tplPage",
            data: {
                lang: JSON.parse(lang).package
            }
        });
        setTimeout(function () {
            initLayui();
        }, 50);

        function initLayui() {
            layui.use(['table', 'layer', 'form','laydate'], function () {
                var layer = layui.layer,
                    table = layui.table,
                    form = layui.form,
                    laydate = layui.laydate;
                laydate.render({
                    elem: '#txtRangeDate'
                    , range: '至'
                });
                var options = {
                    elem: '#table'
                    , url: '/Quality/QueryReworks'
                    , method: "post"
                    , where: { orderNo: "", reworkNo: "", workshopCode: "", reviewStatus: null, produceStatus: null, reworkType: null, startDate: "@DateTime.Today.AddDays(-7).ToString("yyyy-MM-dd")", endDate: "@DateTime.Today.ToString("yyyy-MM-dd")" }
                    , page: {
                        limit: 15
                        , limits: [15, 20, 25, 30, 40, 50]
                    }
                    , response: {
                        statusName: 'ErrCode'
                        , statusCode: 200
                        , msgName: 'ErrCode'
                        , countName: 'TotalCount'
                        , dataName: 'Data'
                    }
                    , cols: [[
                        { title: '序号', type: "numbers", width: 60, fixed: "left" }
                        , { field: 'ReworkNo', title: '返工单号', minWidth: 150, fixed: "left" }
                        , { field: 'CNReworkType', title: '返修类型', minWidth: 100, fixed: "left" }
                        , { field: 'OrderNo', title: '订单号', minWidth: 150, fixed: "left" }
                        , { field: 'CustomerCode', title: '客户代码', minWidth: 150 }
                        , { field: 'CustomerName', title: '客户名称', minWidth: 150 }
                        , { field: 'WorkshopName', title: '返工车间', minWidth: 150 }
                        , {
                            title: '返工工序', minWidth: 100, templet: function (d) {
                                var sites = new Array();
                                for (var i = 0; i < d.Sites.length; i++) {
                                    sites.push(d.Sites[i].SiteName);
                                }
                                return sites.join(", ");
                            }
                        }
                        , { field: 'CreateTimeStr', title: '返工时间', minWidth: 150 }
                        , { field: 'CNReviewStatus', title: '审核状态', minWidth: 100 }
                        , { field: 'CNReviewBy', title: '审核人', minWidth: 100 }
                        , { field: 'ReviewTimeStr', title: '审核时间', minWidth: 150 }
                        , { field: 'CNProduceStatus', title: '生产状态', minWidth: 100 }
                        , {
                            title: '操作', width: 300, fixed: "right", templet: function (d) {
                                var str = '<button class="layui-btn layui-btn-sm layui-btn-warm" onclick="reworkList(\'' + d.ReworkNo + '\')"><i class="layui-icon">&#xe614;</i>返工清单</button>';
                                if (d.ReviewStatus == 0 || d.ReviewStatus == 3) {
                                    str = str + '<button class="layui-btn layui-btn-sm layui-btn-normal" onclick="editReworkOrder(\'' + d.ReworkNo + '\')"><i class="layui-icon">&#xe642;</i>编辑</button>\
                                                 <button class="layui-btn layui-btn-sm layui-btn-normal" onclick="submitReview(\'' + d.ReworkNo + '\')"><i class="layui-icon">&#xe652;</i>提交审核</button>';
                                }
                                return str;
                            }
                        }
                    ]]
                };

                $("#btnSearch").click(function () {
                    queryTable();
                });
                queryTable();
                function queryTable() {
                    options.page.curr = 1;
                    options.where.orderNo = $("#txtOrderNo").val();
                    options.where.reworkNo = $("#txtReWorkOrderNo").val();
                    options.where.workshopCode = $("#selWorkshops").val();
                    options.where.reviewStatus = $("#selReviewStatus").val() == "-1" ? null : parseInt($("#selReviewStatus").val());
                    options.where.produceStatus = $("#selProduceStatus").val() == "-1" ? null : parseInt($("#selProduceStatus").val());
                    var rangeTime = $("#txtRangeDate").val().trim() == '' ? new Array() : $("#txtRangeDate").val().trim().split(' 至 ');
                    options.where.startDate = rangeTime.length <= 0 ? null : rangeTime[0];
                    options.where.endDate = rangeTime.length <= 0 ? null : rangeTime[1];

                    if (table.index <= 0) {
                        table.render(options);
                    } else {
                        table.reload("table", options);
                    }
                }
            });
        }

        function editReworkOrder(reworkNo) {
            layer.open({
                type: 2,
                shade: [0.1, '#000'],
                area: ['550px', '550px'],
                content: "/Quality/ReworkEdit?reworkNo=" + reworkNo,
                end: function () {
                    $("#btnSearch").click();
                }
            });
        }

        function submitReview(reworkNo) {
            layer.confirm('确定要提交审核吗？', { icon: 3, title: '提示' }, function (index) {
                var loadId = layer.load();
                $.ajax({
                    url: "/Quality/SubmitReview",
                    data: {
                        reworkNo: reworkNo
                    },
                    complete: function () {
                        layer.close(loadId);
                    },
                    success: function (res) {
                        if (res.ErrCode != 200) {
                            layer.msg(res.ErrMsg, { icon: 5 });
                            return;
                        }
                        layer.msg("提交成功！", { icon: 6, time: 1500 });
                        $("#btnSearch").click();
                    }
                });
                layer.close(index);
            });
        }

        function reworkList(reworkNo) {
            parent.layui.index.openTabsPage("/Quality/ReworkItemList?reworkNo=" + reworkNo, "返工清单(" + reworkNo + ")");
        }
    </script>
}
